<?php

namespace front\modules\web\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use order\services\OrderUserService;
use yii\helpers\ArrayHelper;

/**
 * OrderusersController implements the CRUD actions for OrderusersModel model.
 */
class OrderusersController extends WebBaseController
{
    public $enableCsrfValidation = false;


    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 创建收货地址
     *     $data = [
    //            'name' => '小蜜蜂',
    //            'district' => 1013,
    //            'address' => '宜山路1000号518室',
    //            'tel' => '13112345678',
    //            'mobile' => '56131214',
    //        ];
     */
    function actionCreate(){
        $data = $this->getData();
        $info = OrderUserService::addAddress($data);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'操作成功',$info['result']);
        }
    }

    /**
     * 查找地址该地址的信息
     */
    function actionFindone(){
        $data = yii::$app->request->post();
        if(!$data['id']){
            WebBaseController::getAjaxData(2,'地址不存在');
        }
        $info = OrderUserService::search($data);
        $info['result'] = ArrayHelper::toArray($info['result']);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'地址获取成功',$info['result']);
        }
    }

    /**
     * 查找该用户所有的地址
     */
    function actionUsersite(){
        $data = yii::$app->request->get();
        if(!$data['user_id']){
            WebBaseController::getAjaxData(2,'用户不存在');
        }
        $info = OrderUserService::search($data);
        $info['result'] = ArrayHelper::toArray($info['result']);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'地址获取成功',$info['result']);
        }
    }


    /**
     * 更新地址
     */
    function actionUpdate(){
        $data = $this->getData();
        $data['id'] = intval(yii::$app->request->post('id'));
        if(!$data['id']){
            WebBaseController::getAjaxData(7,'地址不存在');
        }
        $model = new OrderUserService();
        $info = $model->update($data);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'操作成功',$info['result']);
        }
    }


    function getData(){
        $data['name'] = trim(yii::$app->request->post('name'));
        $data['district'] = trim(yii::$app->request->post('district'));
        $data['address'] = yii::$app->request->post('address');
        $data['tel'] = yii::$app->request->post('tel');
        $data['mobile'] = yii::$app->request->post('mobile') ? trim(yii::$app->request->post('mobile')) :'';
        $data['user_id'] = $this->user_id;
        if(!$data['name']){
            WebBaseController::getAjaxData(2,'收货人不能空');
        }
        if(!$data['district']){
            WebBaseController::getAjaxData(3,'收货地址不能空');
        }
        if(!$data['tel']){
            WebBaseController::getAjaxData(4,'手机号码不能空');
        }
        if(strlen($data['tel'])!=11 || !preg_match('/^1[3|4|5|7|8]\d{9}$/',$data['tel'])){
            WebBaseController::getAjaxData(5,'手机号码格式不正确');
        }
        if(!$data['address']){
            WebBaseController::getAjaxData(6,'详细地址不能空');
        }

        return $data;
    }







}
